home *** CD-ROM | disk | FTP | other *** search
/ Info-Mac 4 / Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso / Graphics / Utilities / CFG 2.1 UR / Palettes / Palette Skeleton (v2.1) / CFG Palette Skeleton.c next >
C/C++ Source or Header  |  1994-03-13  |  7KB  |  289 lines

  1. /**************************************************************************************
  2.     Color Fractal Generator 2.1
  3.     module: CFG Palette Skeleton.c
  4.     September 1992 - March 1994
  5.     by John A. Schlack
  6.  
  7.     Description:
  8.             This module provides a guideline for creating your own CFG palettes.
  9.         All that needs to be done is to fill in the code in create_palette() so that
  10.         the desired colors are created.
  11.         
  12.  **************************************************************************************/
  13.  
  14.  
  15. #include <string.h>
  16.  
  17.  
  18. /**************************************************************************************
  19.     TYPEDEFS
  20.  **************************************************************************************/
  21.  
  22.  
  23. typedef struct identifier_struct    // 128 byte header
  24. {
  25.     char    zero[4];            // first 4 bytes must be 0
  26.     char    appl_name[32];        // application name
  27.     char    by[32];
  28.     OSType    file_type;            // fractal, palette, cusfrac, prefs, etc.
  29.     char    free1[4];            // used by ver 2.0 as part of 8 byte file_type
  30.     OSType    version;            // application version: x.xx
  31.     OSType    earliest_vers;        // earliest app version that can open file
  32.     char    registered[4];
  33.     char    password[8];
  34.     char    free2[32];
  35. } identifier_struct, *identifier_ptr;
  36.  
  37. typedef struct file_palette_header_210
  38. {
  39.     short    ctSize;            // number of entries in color table - 1
  40.     short    userColors;        // # colors user defined
  41.     Str255    load_pal_name;    // name of palette
  42. } file_palette_header_210;
  43.  
  44.  
  45. /**************************************************************************************
  46.     REGIONAL VARIABLES
  47.  **************************************************************************************/
  48.  
  49.  
  50. static char             file_cmp[4] = { '2', '.', '0', '0' };
  51. static char             file_erl[4] = { '2', '.', '0', '0' };
  52.  
  53.  
  54. /**************************************************************************************
  55.     CONSTANTS
  56.  **************************************************************************************/
  57.  
  58.  
  59. #define FILE_CREATOR            'fraG'
  60. #define FILE_TYPE_PALETTE        'fraP'
  61. #define CFG_VERSION                '2.10'
  62. #define CFG_EARLIEST            '2.10'
  63. #define FT_PALETTE                'pltt'
  64.  
  65. #define STD_PALETTE_SIZE        256
  66. #define MAX_USER_COLORS            250
  67.  
  68.  
  69. // THESE ARE OK TO CHANGE: BUT A MAX OF 32 BYTES EACH
  70.  
  71. char *    gStr_file_appl_name = "Color Fractal Generator";
  72. char *    gStr_file_by = "John A. Schlack   Shadowbane";
  73.  
  74.  
  75. /**************************************************************************************
  76.     PROTOTYPES
  77.  **************************************************************************************/
  78.  
  79.  
  80. void             main( void );
  81. void             write_palette_to_file( unsigned char * name, short vRefNum,
  82.                     RGBColor * entries );
  83. void             create_palette( RGBColor * entries );
  84. void            add_standard_colors( RGBColor * entries );
  85. unsigned char *    pstrcpy( unsigned char * dest, unsigned char * src );
  86. void            do_alert( unsigned char * msg );
  87.  
  88.  
  89. /* --------------------------------------------------------------------------------- */
  90.  
  91.  
  92. void main( void )
  93. {
  94.     Point        thePoint = { 40, 40 };
  95.     SFReply        reply;
  96.     RGBColor *    entries;
  97.     
  98.     InitGraf( &thePort );
  99.     InitFonts();
  100.     FlushEvents( everyEvent, 0 );
  101.     InitWindows();
  102.     InitMenus();
  103.     TEInit();
  104.     InitDialogs( 0L );
  105.     InitCursor();
  106.     
  107.     /* allocate memory */
  108.     
  109.     entries = (RGBColor *) NewPtr( STD_PALETTE_SIZE * sizeof( RGBColor ) );
  110.     if (entries == NULL)
  111.     {
  112.         do_alert( "\pNot enough memory to create palette" );
  113.         ExitToShell();
  114.     }
  115.     
  116.     /* keep creating palettes until user hits cancel button */
  117.     for (;;)
  118.     {
  119.         SFPutFile( thePoint, "\pEnter Palette Name", "\pPalette", 0L, &reply );
  120.         if (!(reply.good))
  121.             break;
  122.         memset( entries, 0, sizeof( RGBColor ) * STD_PALETTE_SIZE );
  123.         create_palette( entries + 1 );    /* +1 skips reserved "white" */
  124.         add_standard_colors( entries );
  125.         write_palette_to_file( reply.fName, reply.vRefNum, entries );
  126.     }
  127.     
  128.     /* free memory */
  129.     DisposPtr( (Ptr) entries );
  130. }
  131.  
  132.  
  133. /* -------------------------------------------------------------------------------- */
  134.  
  135.  
  136. void write_palette_to_file( unsigned char * name, short vRefNum, RGBColor * entries )
  137. {
  138.     identifier_struct        ID;
  139.     file_palette_header_210    pal_head;
  140.     long                    siz;
  141.     short                    srcFile;
  142.     OSErr                    theErr;
  143.  
  144.     /* create the palette file */
  145.     
  146.     theErr = Create( name, vRefNum, FILE_CREATOR, FILE_TYPE_PALETTE );
  147.     if ((theErr != noErr) && (theErr != dupFNErr))
  148.     {
  149.         do_alert( "\pCannot create palette file" );
  150.         return;
  151.     }
  152.  
  153.     /* open the file for writing */
  154.  
  155.     theErr = FSOpen( name, vRefNum, &srcFile );
  156.     if (theErr != noErr)
  157.     {
  158.         do_alert( "\pCannot open palette file" );
  159.         FSDelete( name, vRefNum );
  160.         return;
  161.     }
  162.  
  163.     // ID
  164.  
  165.     memset( &ID, 0, sizeof( ID ) );
  166.     strcpy( ID.appl_name, gStr_file_appl_name );
  167.     strcpy( ID.by, gStr_file_by );
  168.     ID.file_type = FT_PALETTE;
  169.     ID.version = CFG_VERSION;
  170.     ID.earliest_vers = CFG_EARLIEST;
  171.  
  172.     // FILL PALETTE HEADER
  173.  
  174.     pal_head.ctSize = STD_PALETTE_SIZE;
  175.     pal_head.userColors = MAX_USER_COLORS;
  176.     pstrcpy( pal_head.load_pal_name, name );
  177.     
  178.     // WRITE ID
  179.     
  180.     siz = (long) sizeof( identifier_struct );
  181.     theErr = FSWrite( srcFile, &siz, &ID );
  182.     
  183.     // WRITE PALETTE HEADER
  184.     
  185.     if (theErr == noErr)
  186.     {
  187.         siz = (long) sizeof( file_palette_header_210 );
  188.         theErr = FSWrite( srcFile, &siz, &pal_head );
  189.     }
  190.     
  191.     // WRITE PALETTE
  192.     
  193.     if (theErr == noErr)
  194.     {
  195.         siz = sizeof( RGBColor ) * (pal_head.ctSize + 1L);
  196.         theErr = FSWrite( srcFile, &siz, entries );
  197.     }
  198.     
  199.     // CLEAN UP
  200.  
  201.     GetFPos( srcFile, &siz );
  202.     SetEOF( srcFile, siz );
  203.     FSClose( srcFile );
  204.  
  205.     if (theErr != noErr)
  206.     {
  207.         do_alert( "\pError while writing to palette file" );
  208.         FSDelete( name, vRefNum );
  209.     }
  210.  
  211.     FlushVol( 0L, vRefNum );
  212. }
  213.  
  214.  
  215. /* --------------------------------------------------------------------------------- */
  216.  
  217.  
  218. void create_palette( RGBColor * entries )
  219. {
  220.     short         i;
  221.     RGBColor    rgb;
  222.     
  223.     /*
  224.         Do not change the loop counter of index to entries.  These values are correct.
  225.         All that needs to be done is to change rgb to the desired color corresponding
  226.         to the palette entry i.
  227.     */
  228.     for (i=0; i<MAX_USER_COLORS; i++)
  229.     {
  230. /* •••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
  231.         SET "rgb" TO DESIRED COLOR
  232.    ••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••• */
  233.         rgb.red = rgb.green = rgb.blue = 0;
  234.         
  235.         entries[i] = rgb;
  236.     }
  237. }
  238.  
  239.  
  240. /* --------------------------------------------------------------------------------- */
  241.  
  242.  
  243. void add_standard_colors( RGBColor * entries )
  244. {
  245.     RGBColor    rgb;
  246.     
  247.     rgb.red = rgb.blue = rgb.green = 0xFFFF;    /* white is first entry */
  248.     entries[0] = rgb;
  249.     
  250.     rgb.red = rgb.blue = rgb.green = 0;            /* reserved */
  251.     entries[STD_PALETTE_SIZE-5] = rgb;
  252.     
  253.     rgb.red = rgb.blue = rgb.green = 0xD000;    /* light gray */
  254.     entries[STD_PALETTE_SIZE-4] = rgb;
  255.     
  256.     rgb.red = rgb.blue = rgb.green = 0xA000;    /* medium gray */
  257.     entries[STD_PALETTE_SIZE-3] = rgb;
  258.     
  259.     rgb.red = rgb.blue = rgb.green = 0x5000;    /* dark gray */
  260.     entries[STD_PALETTE_SIZE-2] = rgb;
  261.     
  262.     rgb.red = rgb.blue = rgb.green = 0;            /* black is last entry */
  263.     entries[STD_PALETTE_SIZE-1] = rgb;
  264. }
  265.  
  266.  
  267. /* -------------------------------------------------------------------------------- */
  268.  
  269.  
  270. unsigned char * pstrcpy( unsigned char * dest, unsigned char * src )
  271. {
  272.     register short    i = *src, j;
  273.     unsigned char *    ret = dest;
  274.  
  275.     for (j=0; j<=i; j++)    /* must include size byte, so has equal sign */
  276.         *dest++ = *src++;
  277.     return dest;
  278. }
  279.  
  280.  
  281. /* -------------------------------------------------------------------------------- */
  282.  
  283.  
  284. void do_alert( unsigned char * msg )
  285. {
  286.     ParamText( msg, 0L, 0L, 0L );
  287.     CautionAlert( 500, 0L );
  288. }
  289.